<?php
namespace Admin\Controller;

use Common\Controller\HighchartsController;

class CensusController extends HighchartsController {
	
    public function index(){
    	$request=I('request.');
    	$start_time=I('request.start_time');
    	if(!empty($start_time)){
    		$where1['RegisterDate'][0]=array(array('EGT',$start_time));
    		$where2['G_TIME'][0]=array(array('EGT',$start_time));
    	}
    	$end_time=I('request.end_time');
    	if(!empty($end_time)){
    		$where1['RegisterDate'][1]=array(array('ELT',$end_time));
    		$where2['G_TIME'][1]=array(array('ELT',$end_time));
    	}
    	$info['adduser'] = M('Accountsinfo',null,'DB_CONFIG2')->where($where1)->count();
    	$info['salcard'] = M('Jinshun_cardtransaction',null)->where($where2)->sum("g_glodnumber");
    	$info['actcard'] = M('Jinshun_glodtransaction',null)->where($where2)->sum("g_glodnumber");
		$addcount = $this->addcount();
        $this->assign("adduser",$addcount['adduser']);
        $this->assign("loguser",$addcount['loginuser']);
        $this->assign("sumuser",$addcount['sumuser']);
        $this->assign("dueuser",$addcount['dueuser']);
        $this->assign("agentcount",$addcount['agent']);
    	$this->assign('info', $info);
    	$this->display();
    }
    public function addcount(){
    	//php获取今日开始时间戳和结束时间戳
    	$beginToday=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y')));
    	$endToday=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1);
    	//php获取昨日起始时间戳和结束时间戳
    	$beginYesterday=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')-1,date('Y')));
    	$endYesterday=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y'))-1);
    	//php获取前天起始时间戳和结束时间戳
    	$beginQianday=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')-2,date('Y')));
    	$endQianday=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')-1,date('Y'))-1);
    	
    	//用户总数
    	$sumjin['RegisterDate']=array(array('ELT',$endToday));
    	$record['sumuser']['jin'] = M('Accountsinfo',null,'DB_CONFIG2')->where($sumjin)->count();
    	$sumzuo['RegisterDate']=array(array('ELT',$endYesterday));
    	$record['sumuser']['zuo'] = M('Accountsinfo',null,'DB_CONFIG2')->where($sumzuo)->count();
    	$sumqian['RegisterDate']=array(array('ELT',$endQianday));
    	$record['sumuser']['qian'] = M('Accountsinfo',null,'DB_CONFIG2')->where($sumqian)->count();
    	
    	//新增用户
    	$infojin['RegisterDate'][0]=array(array('EGT',$beginToday));
    	$infojin['RegisterDate'][1]=array(array('ELT',$endToday));
    	$record['adduser']['jin'] = M('Accountsinfo',null,'DB_CONFIG2')->where($infojin)->count();
    	$infozuo['RegisterDate'][0]=array(array('EGT',$beginYesterday));
    	$infozuo['RegisterDate'][1]=array(array('ELT',$endYesterday));
    	$record['adduser']['zuo'] = M('Accountsinfo',null,'DB_CONFIG2')->where($infozuo)->count();
    	$infoqian['RegisterDate'][0]=array(array('EGT',$beginQianday));
    	$infoqian['RegisterDate'][1]=array(array('ELT',$endQianday));
    	$record['adduser']['qian'] = M('Accountsinfo',null,'DB_CONFIG2')->where($infoqian)->count();
    	
    	//活跃用户
    	$logjin['LastLogonDate'][0]=array(array('EGT',$beginToday));
    	$logjin['LastLogonDate'][1]=array(array('ELT',$endToday));
    	$record['loginuser']['jin'] = M('Accountsinfo',null,'DB_CONFIG2')->where($logjin)->count();
    	$logzuo['LastLogonDate'][0]=array(array('EGT',$beginYesterday));
    	$logzuo['LastLogonDate'][1]=array(array('ELT',$endYesterday));
    	$record['loginuser']['zuo'] = M('Accountsinfo',null,'DB_CONFIG2')->where($logzuo)->count();
    	$logqian['LastLogonDate'][0]=array(array('EGT',$beginQianday));
    	$logqian['LastLogonDate'][1]=array(array('ELT',$endQianday));
    	$record['loginuser']['qian'] = M('Accountsinfo',null,'DB_CONFIG2')->where($logqian)->count();
    	
    	//对决场数
    	$duejin['CostDate'][0]=array(array('EGT',$beginToday));
    	$duejin['CostDate'][1]=array(array('ELT',$endToday));
    	$duejin['CostValue']=array(array('gt',0));
    	$record['dueuser']['jin'] = M('Recordprivatecost',null,'DB_CONFIG5')->where($duejin)->count();
    	$duezuo['CostDate'][0]=array(array('EGT',$beginYesterday));
    	$duezuo['CostDate'][1]=array(array('ELT',$endYesterday));
    	$duezuo['CostValue']=array(array('gt',0));
    	$record['dueuser']['zuo'] = M('Recordprivatecost',null,'DB_CONFIG5')->where($duezuo)->count();
    	$dueqian['CostDate'][0]=array(array('EGT',$beginQianday));
    	$dueqian['CostDate'][1]=array(array('ELT',$endQianday));
    	$dueqian['CostValue']=array(array('gt',0));
    	$record['dueuser']['qian'] = M('Recordprivatecost',null,'DB_CONFIG5')->where($dueqian)->count();
    	
    	return $record;
    }
    
    public function quartercount(){
    	$record['chart']['type'] = "column";
    	$record['title']['text'] = "季度统计";
    	$record['xAxis']['categories'] = 1;
    	$record['series']['type']=1;
		$quart = $this->getQuarter();
		$info = M('Accountsinfo',null,'DB_CONFIG2');
		$sale = M('Jinshun_cardtransaction',null);
		$action = M('Jinshun_glodtransaction',null);
		if(!empty($quart) && is_array($quart)){
			foreach ($quart as $key=>$val){
				$start_time=$val['start'];
				if(!empty($start_time)){
					$where1['RegisterDate'][0]=array(array('EGT',$start_time));
					$where2['G_TIME'][0]=array(array('EGT',$start_time));
				}
				$end_time=$val['end'];
				if(!empty($end_time)){
					$where1['RegisterDate'][1]=array(array('ELT',$end_time));
					$where2['G_TIME'][1]=array(array('ELT',$end_time));
				}
				$record['series']['data'][$key]['adduser'] = $info->where($where1)->count();
				$sale_num = $sale->where($where2)->sum("g_glodnumber");
				$actn_num = $action->where($where2)->sum("g_glodnumber");
				$record['series']['data'][$key]['salcard'] = intval($sale_num);
				$record['series']['data'][$key]['actcard'] = intval($actn_num);
			}
		}
		$this->highcharts($record);
    }
    public function monthcount(){
    	$record['chart']['type'] = "column";
    	$record['title']['text'] = "月度统计";
    	$record['xAxis']['categories'] = 2;
    	$record['series']['type']=2;
		$month = $this->getMonth();
		$info = M('Accountsinfo',null,'DB_CONFIG2');
		$sale = M('Jinshun_cardtransaction',null);
		$action = M('Jinshun_glodtransaction',null);
		if(!empty($month) && is_array($month)){
			foreach ($month as $key=>$val){
				$start_time=$val['start'];
				if(!empty($start_time)){
					$where1['RegisterDate'][0]=array(array('EGT',$start_time));
					$where2['G_TIME'][0]=array(array('EGT',$start_time));
				}
				$end_time=$val['end'];
				if(!empty($end_time)){
					$where1['RegisterDate'][1]=array(array('ELT',$end_time));
					$where2['G_TIME'][1]=array(array('ELT',$end_time));
				}
				$record['series']['data'][$key]['adduser'] = $info->where($where1)->count();
				$sale_num = $sale->where($where2)->sum("g_glodnumber");
				$actn_num = $action->where($where2)->sum("g_glodnumber");
				$record['series']['data'][$key]['salcard'] = intval($sale_num);
				$record['series']['data'][$key]['actcard'] = intval($actn_num);
			}
		}
		$this->highcharts($record);
    }
    public function getQuarter(){//获得季度
    	$son = ceil((date('n'))/3);
    	for($i=1;$i<=$son;$i++){
    		$quarter[$i]['start'] = date('Y-m-d H:i:s', mktime(0, 0, 0,$i*3-3+1,1,date('Y')));
    		$quarter[$i]['end'] = date('Y-m-d H:i:s', mktime(23,59,59,$i*3,date('t',mktime(0, 0 , 0,$i*3,1,date("Y"))),date('Y')));
    	}
    	return $quarter;
    }
    
    public function getMonth(){//获得月份
    	$m = date("m");
    	for($i=0;$i<$m;$i++){
	    	$month[$m-$i]['start'] =  date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-$i,1,date("Y")));
	    	$month[$m-$i]['end'] =  date("Y-m-d H:i:s",mktime(23,59,59,date("m")+1-$i,0,date("Y")));
    	}
    	return $month;
    }
}